function get_time(content) {
    const regex = /Elapsed \(wall clock\) time \(h:mm:ss or m:ss\): (\d+):(\d+\.\d+)/;
    const match = content.match(regex);

    if (match) {
        const minutes = parseInt(match[1]);
        const seconds = parseFloat(match[2]);
        const totalSeconds = minutes * 60 + seconds;
        // console.log("Total seconds:", totalSeconds);
        return totalSeconds;
    } else {
        const regex = /Elapsed \(wall clock\) time \(h:mm:ss or m:ss\): \-(\d+):\-(\d+\.\d+)/;
        const match = content.match(regex);
        if (match) {
            return -1;
        }
        console.log(content);
        throw new Error("Elapsed time not found.");
    }
}

function get_memory(content) {
    const regex = /Maximum resident set size \(kbytes\): (\d+)/;
    const match = content.match(regex);

    if (match) {
        const residentSetSize = parseInt(match[1]);
        // console.log("Maximum resident set size:", residentSetSize);
        return residentSetSize;
    } else {
        console.log(content);
        throw new Error("Maximum resident set size not found.");
    }
}

function processTimeDetailInfo(data) {
    if (data.slice(data.length-100).match(/Exit status: (\d+)/))
        return {
            time: get_time(data),
            memory: get_memory(data),
            // assertFail: data.includes("Assertion failed"),
        };
    else
    {
        return {time: -1, memory: -1};
    }
}

module.exports = {
    processTimeDetailInfo,
};
